(function(){
	var wiky=window.wiky=window.$=function(selector){
		return new wiky.core.init(selector);
	};
	wiky.core=wiky.prototype={
		init:function(selector){
			selector = selector || document;
			if ( selector.nodeType ) {
				this[0] = selector;
				return this;
			}
			if(typeof selector=="string")
				this[0]=document.getElementById(selector);
			return this;
		},
		
		ready:function(fn){
			wiky.event.loadEvent(fn);
			return this;
		},

		html: function(val){
			if(typeof val != 'undefined'){				
				this[0].innerHTML = val;
			}
			return this[0].innerHTML;
		},
		
		style:function(prop,val){
			if(typeof val != 'undefined'){				  
				this[0].style[prop]=val;
				return this;
			}else return wiky.dom.getStyle(this[0], prop);
			
		},
		
		hasClass:function(className){
			return wiky.dom.hasClass(this[0], className);
		},
		
		addClass: function(className) {
            wiky.dom.addClass(this[0],className);
			return this;
        },

        removeClass: function(className) {
            wiky.dom.removeClass(this[0],className);
			return this;
        },
		
		bind:function(type,handler){
			wiky.event.addEvent(this[0],type,handler);
			return this;
		},
		
		show:function(){   
			this.style("display","block");     
			return this;   
		},
		
		hide:function(){   
			this.style("display","none"); 
			return this;   
		},
		
		drag: function(oRoot){
			new wiky.drag.init(this[0], oRoot);
			return this;
		},
		
		setOpacity: function(level){
			if(this[0].filters){
				this[0].style.filter = 'alpha(opacity =' + level + ')';
			}
			else this[0].style.opacity = level / 100;
			return this;
		},
		
		slide:function(options){		
			var fps=30,step=0;
			var opt={
				from:0,
				prop:"height",
				duration:1000
			};
			var val = parseInt(wiky.dom.getStyle(this[0],opt.prop));
			if(val!=0){opt.to=0;opt.from=val;}
			
			if(typeof options != 'undefined'){	
				for(var key in options)opt[key]=options[key];
			}
			if(opt.from==opt.to)return this;this.show();
			this[0].style[opt.prop]=opt.from+"px";
			var numsteps=opt.duration / 1000 * fps;
			var interval= (opt.from - opt.to) / numsteps;
			(function(el){
				function fn(){
					var newval=opt.from-(step*interval);
					if(step++<numsteps)el[0].style[opt.prop]=Math.ceil(newval)+"px";
					else{
						el[0].style[opt.prop]=opt.to+"px";
						clearInterval(intervalID);
						if(newval==0){
							el.hide();
							el[0].propVal=val;
						}		
					}
				}
				var intervalID=setInterval(fn,1000/fps);
				
			})(this);
			return this;
		},
		
		slideUp:function(speed){
			var w=parseInt(this.style("height"));
			var d=1000;
			if(typeof speed != 'undefined'){				
				if(speed == "fast")d = 500;
				else if(speed == "slow")d = 2500;
				else d=parseInt(speed);
			}
			var opt={
				from:w,
				to:0,
				prop:"height",
				duration:d
			};
			
			return this.slide(opt);
		},
		
		slideDown:function(speed,to){
			var v=this[0].propVal;
			var d=1000;
			if(typeof speed != 'undefined'){				
				if(speed=="fast")d=500;
				else if(speed=="slow")d=2500;
				else d=parseInt(speed);
			}
			var opt={
				from:0,
				to:to||v||parseInt(this.style("height")),
				prop:"height",
				duration:d
			};
			this.show();
			return this.slide(opt);
		},
		
		fadeOut:function(){
			this.setOpacity(0);
			for(var i=0;i<=100;i+=4){
				(function(el){
					var pos=i;
					setTimeout(function(){el.setOpacity(pos)},(pos+1)*10);
						
				})(this);
			}
			return this;
		},
		
		fadeIn:function(){
			this.setOpacity(100);
			for(var i=100;i>=0;i-=4){
				(function(el){
					var pos=i;
					setTimeout(function(){el.setOpacity(pos)},(99-pos)*10);
						
				})(this);
			}
			return this;
		},
		
		getMousePos: function(e){
			return wiky.event.getMousePos(e);
		},
		
		pageXY:function(pos){
			if(typeof pos != "undefined"){
				wiky.dom.setPageXY(this[0], pos);
				return this;
			}else
			return wiky.dom.getPageXY(this[0]);
		}
	};
	wiky.core.init.prototype=wiky.core;
	
	wiky.extend = wiky.core.extend = function(obj,prop) {
		if( !prop ) {   
			prop = obj;   
			obj = this;   
		}
		for (var i in prop )   
			obj[ i ] = prop[ i ];
		return obj;   
	};
	
	wiky.extend(wiky.prototype,{
		
		get:function(name){
			return this[0][name];
		},
		
		val:function(val){
			if(typeof val!="undefined"){
				this[0].value="";
				this[0].value+=val;
				return this;
			}else{
				return this[0].value
			}
		}
	});
	
	wiky.drag = {
		init:function(o, oRoot){
			wiky.drag.obj = o;
			if(typeof oRoot=="string")
				oRoot=document.getElementById(oRoot);
			o.root = oRoot && oRoot != null ? oRoot : o;
			wiky.dom.setStyle(o.root, "position", "absolute");
			wiky.dom.setStyle(o, "cursor", "move");
			
			
			o.onmousedown = wiky.drag.start;
		},
		start:function(e){
			var o = wiky.drag.obj = this; 
			
			o.root.oldIndex=parseInt(wiky.dom.getStyle(o.root,"zIndex"));
			wiky.dom.setStyle(o.root,"zIndex",999);
			var m = wiky.event.getMousePos(e);
			o.lastMouseX = m[0];
			o.lastMouseY = m[1];
			document.onmousemove = wiky.drag.drag;
			document.onmouseup = wiky.drag.end;
			
			return this;
		},
		drag:function(e){
			var o = wiky.drag.obj,
			nx, ny;
			var m = wiky.event.getMousePos(e);
			var ex = m[0],ey = m[1],
			xy = wiky.dom.getPageXY(o.root),
			x=xy[0],y=xy[1];
			nx = x + (ex - o.lastMouseX);
			ny = y + (ey - o.lastMouseY);
			o.root.style.left = nx + "px";
			o.root.style.top = ny + "px";
			o.lastMouseX = ex;
			o.lastMouseY = ey;
			return false;
		},
		end:function(){
			var o = wiky.drag.obj;
			wiky.dom.setStyle(o.root,"zIndex",o.root.oldIndex||0);
			document.onmousemove = null;
			document.onmouseup = null;
		}
	};
	wiky.dom = {
		id: function(node){
			return document.getElementById(node);
		},
		
		tag: function(node, el){
			return (el || document).getElementsByTagName(node);
		},
		
		className: function(node, className, tag) {
            if (!node) {return [];}
			if(tag == null) tag = '*';
            var nodes = new Array(),
                elements = node.getElementsByTagName(tag);

            for (var i = 0, len = elements.length; i < len; i++) {
                if ( this.hasClass(elements[i], className) ) {
                    nodes[nodes.length] = elements[i];
                }
            }

            return nodes;
        },
		
		getStyle: function(el, prop){
			if(el.style[prop])
				return el.style[prop];
			else if(el.currentStyle)
				return el.currentStyle[prop];
			else if(document.defaultView && document.defaultView.getComputedStyle){
				prop = prop.replace(/([A-Z])/g, "-$1");
				prop = prop.toLowerCase();
				var s = document.defaultView.getComputedStyle(el, "");
				return s && s.getPropertyValue(prop);
			}
			else
				return null;		  
		},
		setStyle: function(el, prop, val){
			el.style[prop] = val;
		},
		
		getPageXY: function(el) {
			return [
				el.offsetParent ? el.offsetLeft + wiky.dom.getPageXY(el.offsetParent)[0] : el.offsetLeft,
				el.offsetParent ? el.offsetTop + wiky.dom.getPageXY(el.offsetParent)[1] : el.offsetTop
			];
		},
		
		setPageXY:function(el,pos){
			var style_pos = wiky.dom.getStyle(el, 'position');
                if (style_pos == 'static') {
                    wiky.dom.setStyle(el, 'position', 'relative');
                    style_pos = 'relative';
                }
                var pageXY = wiky.dom.getPageXY(el);
                var delta = [
                    parseInt( wiky.dom.getStyle(el, 'left'), 10 ),
                    parseInt( wiky.dom.getStyle(el, 'top'), 10 )
                ];
                if ( isNaN(delta[0]) ) {
                    delta[0] = (style_pos == 'relative') ? 0 : el.offsetLeft;
                } 
                if ( isNaN(delta[1]) ) {
                    delta[1] = (style_pos == 'relative') ? 0 : el.offsetTop;
                } 
                if (pos[0] !== null) { el.style.left = pos[0] - pageXY[0] + delta[0] + 'px'; }
                if (pos[1] !== null) { el.style.top = pos[1] - pageXY[1] + delta[1] + 'px'; }
				var newXY = wiky.dom.getPageXY(el);
				if ( (pos[0] !== null && newXY[0] != pos[0]) || (pos[1] !== null && newXY[1] != pos[1]) ) {
					wiky.dom.setPageXY(el, pos);
				}
		},
		
		hasClass: function(el, className) {
            if(className && (' ' + el.className + ' ').indexOf(' ' + className + ' ') > -1)return true;
			else return false;
        },

        addClass: function(el, className) {
            if (el.className === '') {
                el.className = className;
            }else if (el.className !== '' && !wiky.dom.hasClass(el, className)) {
                el.className = el.className + ' ' + className;
            }
        },

        removeClass: function(el, className) {
            if (wiky.dom.hasClass(el, className)) {
                el.className = (' ' + el.className + ' ').replace(' ' + className + ' ', ' ').replace(/^ | $/g,'');
            }
        }
	};
	wiky.event={
		loadEvent:function(fn) {
			var oldonload = window.onload; 
			if (typeof window.onload != 'function') {
				window.onload = fn;
			}else {
				window.onload = function() {
					oldonload(); 
					fn();
				} 
			} 
		},
		
		addEvent:function(el, ev, handler){
			if (el.addEventListener) {
				el.addEventListener(ev, handler, false);
			} else if (el.attachEvent) {
				el.attachEvent('on' + ev, function() {handler.call(el)});
			}
		},
		
		stopEvent: function(ev) {
           this.stopPropagation(ev);
           this.preventDefault(ev);
        },
		
        stopBubble: function(ev) {
           if (ev.stopPropagation) {
               ev.stopPropagation();
           } else {
               ev.cancelBubble = true;
           }
        },
		
        preventDefault: function(ev) {
           if (ev.preventDefault) {
               ev.preventDefault();
           } else {
               ev.returnValue = false;
           }
        },
		
        getEvent: function(e) {
           var ev = e || window.event;
           if (!ev) {
               var c = this.getEvent.caller;
               while (c) {
                   ev = c.arguments[0];
                   if (ev && Event == ev.constructor) {
                       break;
                   }
                   c = c.caller;
               }
           }
           return ev;
        },
		
		getMousePos: function(e){
			var ev =this.getEvent(e);
			if(ev.pageX || ev.pageY){
			  return [ev.pageX, ev.pageY];
			}
			return [
			  ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			  ev.clientY + document.body.scrollTop - document.body.clientTop
			]
		}
	};
	wiky.extend(wiky.prototype,{
		setCookie: function(options){
			wiky.cookie.setCookie(options);
			return this;
		},
		getCookie: function(name){
			return wiky.cookie.getCookie(name);
		},
		delCookie: function(name){
			wiky.cookie.delCookie(name);
			return this;
		}
	
	});
	wiky.cookie={
		setCookie: function(opt) {
	        var str = opt.name + "=" + escape(opt.value) + ";";
			if(opt.days) str += "expires="+new Date(new Date().getTime() + opt.days*86400000).toGMTString()+"; ";
         	if(opt.domain)str+="domain="+opt.domain+"; ";
         	if(opt.path)str+="path="+opt.path+"; ";
         	if(opt.secure)str+="secure;" ;
         	document.cookie=str;
     	},

     	getCookie: function(name) {
        	var value = "";
        	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
        	if(arr != null) { value=unescape(arr[2]); }
        	return value;
     	},
		
     	delCookie: function(name){
        	var exp = new Date();
			exp.setTime(exp.getTime() - 100000);
			var cval=wiky.cookie.getCookie(name);
			if(cval!=null) document.cookie= name + "=;expires="+exp.toGMTString(); 
     	}
	};
	wiky.extend(wiky.prototype,{
		create: function(){
			return wiky.ajax.createXHR();
		},
		ajax: function(url, options){
			wiky.ajax.send(url,options);
			return this;
		}
	});
	wiky.ajax={
		
		createXHR: function() {
			var xhr=null;
			if(window.XMLHttpRequest){
				xhr=new XMLHttpRequest();
			} else {
				try {
					xhr = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e) {
					try {
						xhr = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e) {
						xhr = null;
					}
				}
			}
			if(!xhr)return;
			return xhr;
		},

		send: function(url, options){
			if(!wiky.ajax.createXHR())return;
			var xhr = wiky.ajax.createXHR();
			var _options = {
				method: 'GET',
				querystring: '',
				onerror: function() {},
				onsuccess: function() {}
			};
			
			for(var key in options) {
				_options[key] = options[key];
			}
			
			function onreadystateCallback() {
				if(xhr.readyState == 4) {
					if(xhr.status >=200 && xhr.status <300){
						_options.onsuccess(xhr);
					} else {
						_options.onerror(xhr);
					}
				}
			}
			
			xhr.open(_options.method, url, true);
			xhr.onreadystatechange = onreadystateCallback;
			xhr.send(_options.querystring);
		}
			
	};
})();